﻿using System;
using UtilZ.Dotnet.Ex.Base;

namespace UtilZ.Dotnet.Ex.Communication.Net.Web
{
    /// <summary>
    /// http请求方法基类
    /// </summary>
    public abstract class HttpMethodAbsAttribute : Attribute
    {
        /// <summary>
        /// http请求方法名
        /// </summary>
        public string Method { get; private set; }

        /// <summary>
        /// 路由模板(eg:[Route("api/v1/[controller]")])
        /// </summary>
        public string Template { get; private set; }

        /// <summary>
        /// 自定义数据转换器
        /// </summary>
        public IHttpDataConverter Converter { get; private set; }

        private Type _converterType = null;
        /// <summary>
        /// 自定义数据转换器类型
        /// </summary>
        public Type ConverterType
        {
            get { return _converterType; }
            set
            {
                _converterType = value;
                if (value == null)
                {
                    this.Converter = ActivatorEx.CreateInstance<IHttpDataConverter>(value);
                }
            }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="method">http请求方法</param>
        /// <param name="template">路由模板</param>
        public HttpMethodAbsAttribute(string method, string template)
        {
            this.Method = method;
            this.Template = template;
        }

        internal void UpdateTemplate(string template)
        {
            this.Template = template;
        }

        internal void SetConverter(IHttpDataConverter converter)
        {
            if (converter != null)
            {
                this.Converter = converter;
                this._converterType = converter.GetType();
            }
        }
    }
}
